//为你推荐轮播
var rewidth,
    i = 0

$(" .recommend ").on("click", ".right-btn", function() {
    rewidth = $(".recommend ul li").width()
    i++
    if (i >= 5) {
        i = 5
        $(this).css({
            opacity: .5
        })
    } else {
        $(this).css({
            opacity: 1
        })
        $(" .recommend .left-btn").css({
            opacity: 1
        })
    }
    $(".recommend ul ").animate({
        marginLeft: -i * rewidth
    }, 300)
})
$(" .recommend ").on("click", ".left-btn", function() {
        rewidth = $(".recommend ul li").width()

        i--
        if (i <= 0) {
            i = 0
            $(this).css({
                opacity: .5
            })
        } else {
            $(this).css({
                opacity: 1
            })
            $(" .recommend .right-btn").css({
                opacity: 1
            })
        }
        $(".recommend ul ").animate({
            marginLeft: -i * rewidth
        }, 300)
    })
    // 购物车
    //加入购物车
var proList
render()
$("#shops").click(function(e) {
    var e = e || window.event,
        target = e.target,
        flag = true
    if (target.nodeName == "BUTTON") {

        // 首先获取localStorage 中有没有proList 如果没有执行的是 添加一个新的对象
        proList = localStorage.getItem("proList");
        // 获取 元素标签中所有的data- 属性值
        var dataset = target.parentNode.dataset
        if (proList == null) {
            proList = [];
        } else {
            // 这里是已经有数据了 但是不知道我们点的商品 在数据中是否存在
            proList = JSON.parse(proList)
            for (var i = 0; i < proList.length; i++) {
                if (proList[i].goodsid == dataset.goodsid) {
                    proList[i].count++;
                    flag = false
                    break
                }
            }
        }
        if (flag) {
            var obj = {
                goodsid: dataset.goodsid,
                img: dataset.img,
                name: dataset.name,
                price: dataset.price,
                count: 1,
                dis_price: dataset.disprice
            }
            proList.push(obj)
        }

        localStorage.setItem("proList", JSON.stringify(proList))
        render()
    }
})


function render() {
    var proList = JSON.parse(localStorage.getItem("proList"));
    var allprice = 0
    var str = ""
    if (proList && proList.length != 0) {
        for (var i = 0; i < proList.length; i++) {
            var sum_price = proList[i].count * proList[i].price - proList[i].dis_price

            str += `<li data-index = "${i}" data-goodsid="${ proList[i].goodsid}">
            <ul class="clearfix">
                <li class="cart-check">
                    <input id="input${i}" class="inputs input-" data-goodsid= "${ proList[i].goodsid}" type="checkbox" value="#f0544d" />
                    <label for="input${i}" class="label"></label>
                </li>
                <li class="shop-desc clearfix">
                    <div class="img-box"><img src="${ proList[i].img}"></div>
                    <div class="text">
                        <a class="shop-name">${ proList[i].name} </a>
                        <p class="discounts"><span>特价</span>该商品特价促销，不与会员优惠叠加</p>
                        <a href="javascript:;" class="service"><img src="../imgs/baoxiu.png">特选服务</a>
                    </div>
                </li>
                <li class="price">${ proList[i].price}</li>
                <li class="dis-price">${ proList[i].dis_price}</li>
                <li class="btn">
                    <a href="javascript:;" class="minus">-</a>
                    <span class="count" data-goodsid= "${ proList[i].goodsid}">${ proList[i].count}</span>
                    <a href="javascript:;" class="plus">+</a>
                </li>
                <li class="sum-price">${sum_price}</li>
                <li class="operation" >
                    <a href="javascript:;" class="move-like">移入收藏夹</a>
                    <a href="javascript:;" class="del" data-goodsid= "${ proList[i].goodsid}">删除</a>
                </li>
            </ul>
        </li>`
        }
        $("#all-count").html(proList.length)
    } else {

        str = ` <li class="shop-cart-clear">
<img src="../imgs/shopcart.png">
<p>购物车里还没有商品，快 <a href="index.html">去逛逛</a> 吧</p>
</li>`
        $("#all-count").html(0)
    }


    $(".shop-list").html(str)
}

//加减商品数量按钮
$(" .main .shop-list ").on("click", ".plus", function() {

    var html = parseInt($(this).prev().html());
    var price = parseInt($(this).parent().siblings(".price").html());
    var disprice = parseInt($(this).parent().siblings(".dis-price").html())
    var sumprice = parseInt($(".accounts").html())
    var sumcount = parseInt($(".order-count").html())
        // 结算价格/结算数量
    if ($(this).parent().siblings(".cart-check").children(".input-").prop("checked")) {
        var html = parseInt($(this).prev().html());
        sumprice = sumprice + price
        sumcount = sumcount + 1
    }
    html = html + 1
        //小计单个商品数量
    $(this).prev().html(html)
        //小计单个商品总价
    var allprice = html * price - disprice
    $(this).parent().siblings(".sum-price").html(allprice)
        // 结算价格
    $(".accounts").html(sumprice)
        // 结算数
    $(".order-count").html(sumcount)

    //localstorage
    // 获取当前列表的index
    var index = $(this).parent().parent().parent().data("index")
        // console.log(e.target.parentNode.parentNode.parentNode.dataset.index)
    var proList = JSON.parse(localStorage.getItem("proList"))
    proList[index].count++
        localStorage.setItem("proList", JSON.stringify(proList))

})
$(" .main .shop-list ").on("click", ".minus", function() {
        var html = parseInt($(this).next().html());
        var price = parseInt($(this).parent().siblings(".price").html());
        var disprice = parseInt($(this).parent().siblings(".dis-price").html())
        var sumprice = parseInt($(".accounts").html())
        var sumcount = parseInt($(".order-count").html())
        if ($(this).parent().siblings(".cart-check").children(".input-").prop("checked")) {
            var html = parseInt($(this).next().html());
            if (html == 1) {
                sumprice = sumprice - 0
                sumcount = sumcount - 0
            } else {
                sumprice = sumprice - price
                sumcount = sumcount - 1
            }

        }
        html = html - 1
        if (html <= 1) {
            html = 1

        }
        //小计单个商品数量
        $(this).next().html(html)
            //小计单个商品总价
        var allprice = html * price - disprice
        $(this).parent().siblings(".sum-price").html(allprice)
            // 结算价格
        $(".accounts").html(sumprice)
            // 结算数
        $(".order-count").html(sumcount)
            //localstorage

        // 获取当前列表的index
        var index = $(this).parent().parent().parent().data("index")
            // console.log(e.target.parentNode.parentNode.parentNode.dataset.index)
        var proList = JSON.parse(localStorage.getItem("proList"))

        if (proList[index].count === 1) {
            proList[index].count = 1
        } else {
            proList[index].count--
        }
        localStorage.setItem("proList", JSON.stringify(proList))

    })
    // 单件删除按钮
$(" .main .shop-list ").on("click", ".del", function() {
    var sumprice = parseInt($(".accounts").html())
    var sumcount = parseInt($(".order-count").html())
    var html = parseInt($(this).parent().siblings(".btn").children(".count").html())
    var allprice = parseInt($(this).parent().siblings(".sum-price").html())
        //localstorage
        // 获取当前列表的index
    var index = $(this).parent().parent().parent().data("index")
        // console.log(e.target.parentNode.parentNode.parentNode.dataset.index)
    var proList = JSON.parse(localStorage.getItem("proList"))
        // 数组中从某个位置开始截取 
    proList.splice(index, 1)
    $("#all-count").html(proList.length)
    localStorage.setItem("proList", JSON.stringify(proList))
    $(this).parent().parent().parent().remove();
    if ($(this).parent().siblings(".cart-check").children(".input-").prop("checked")) {
        sumprice = sumprice - allprice
        sumcount = sumcount - html
    }
    $(".accounts").html(sumprice)
    $(".order-count").html(sumcount)
    render()

})

//下面多个删除按钮
$(".all-del").click(function() {
    var proList = JSON.parse(localStorage.getItem("proList"))
    var arr = []
    $(".input-").each(function(k, ele) {
        if ($(ele).prop("checked")) {
            var goodsid = $(ele).parent().parent().parent().data("goodsid")
            arr.push(goodsid)
        }
    })
    for (var a = 0; a < arr.length; a++) {
        for (var p = 0; p < proList.length; p++) {
            if (proList[p].goodsid == arr[a]) {
                var index = p
                proList.splice(index, 1)
            }
        }
    }


    localStorage.setItem("proList", JSON.stringify(proList))
    render()
})

// 选择商品
var input1 = document.getElementById("all-input"),
    input2 = document.getElementById("all2-input"),
    inputs = document.getElementsByClassName("input-")
    //全选
function allcheck(dom1, dom2, dom3) {

    dom1.onclick = function() {
        console.log(0);

        var checked = dom1.checked
        dom2.checked = checked
        var arr = dom3
        var sumprice = 0;
        var sumcount = 0
        if (checked) {
            for (var i = 0; i < arr.length; i++) {
                arr[i].checked = true;
                sumprice += parseInt($(arr[i]).parent().siblings(".sum-price").html());
                sumcount += parseInt($(arr[i]).parent().siblings(".btn").children(".count").html());
            }
            $(".accounts").html(sumprice)
            $(".order-count").html(sumcount)
        } else {
            for (var i = 0; i < arr.length; i++) {
                arr[i].checked = false;
            }
            $(".accounts").html(0)
            $(".order-count").html(0)
        }

    }
}
allcheck(input1, input2, inputs);
allcheck(input2, input1, inputs);
//单个选择时全选按钮的状态
function checkone() {
    for (var c = 0; c < inputs.length; c++) {
        if (!inputs[c].checked) {
            input1.checked = false
            input2.checked = false
            break;
        } else {
            input1.checked = true
            input2.checked = true

        }
    }
}
//单个选择
$(".shop-list").on("click", ".input-", function() {
    checkone()
    var sumprice = parseInt($(".accounts").html())
    var sumcount = parseInt($(".order-count").html())
    if ($(this).prop("checked")) {
        sumprice = sumprice + parseInt($(this).parent().siblings(".sum-price").html())
        sumcount = sumcount + parseInt($(this).parent().siblings(".btn").children(".count").html())
    } else {
        sumprice = sumprice - parseInt($(this).parent().siblings(".sum-price").html())
        sumcount = sumcount - parseInt($(this).parent().siblings(".btn").children(".count").html())
    }
    $(".accounts").html(sumprice)
    $(".order-count").html(sumcount)
})